package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkPointGraphController.PointRemark;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkPointGraphController.PointSheet;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkPointGraphController.RespModel;
import com.china08.yxyapi.entity.mg.homework.HomeworkKnowReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalysePointGraphService;

@Service
public class HomeworkInfoAnalysePointGraphServiceImpl implements HomeworkInfoAnalysePointGraphService {

    @Autowired
    private HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Override
    public RespModel convertRespModel(List<HomeworkKnowReport> homeworkKnowReports, String taskId) {
        // return
        RespModel respModel = new RespModel();
        List<PointSheet> pointSheets = new ArrayList<PointSheet>();
        PointRemark pointRemark = new PointRemark();
        // ##
        // 知识点掌握情况
        List<HomeworkTaskQues> homeworkTaskQuesLst = homeworkTaskQuesRepository.findByTaskId(taskId);
        Map<Integer, String> knowMap = new HashMap<Integer, String>();
        homeworkTaskQuesLst.forEach(homeworkTaskQues -> {
            List<Integer> knowIds = homeworkTaskQues.getKnowId();
            List<String> knowNames = homeworkTaskQues.getKnowName();
            if (knowIds != null && knowIds.size() > 0) {
                for (int i = 0; i < knowIds.size(); i++) {
                    knowMap.put(knowIds.get(i), knowNames.get(i));
                }
            }
        });
        Set<Integer> knowIdSet = knowMap.keySet();
        knowIdSet.forEach(knowId -> {
            // ##
            Double rate = 0d;
            int n = 0;
            if (homeworkKnowReports != null && homeworkKnowReports.size() > 0) {
                for (HomeworkKnowReport homeworkKnowReport : homeworkKnowReports) {
                    if (knowId == homeworkKnowReport.getKnowId() || knowId.equals(homeworkKnowReport.getKnowId())) {
                        rate = rate + homeworkKnowReport.getRate();
                        n++;
                    }
                }
            }
            // ##
            PointSheet pointSheet = new PointSheet();
            pointSheet.knowId = knowId;
            pointSheet.knowName = knowMap.get(knowId);
            pointSheet.rate = n > 0 ? this.getDouble(rate, n) : 0;
            pointSheets.add(pointSheet);
        });
        // ##
        // 知识点结果点评
        int status = -1;
        for (PointSheet pointSheet : pointSheets) {
            if (pointSheet.rate < 60) {
                status = 1;
                continue;
            } else {
                if (status == 1) {
                    break;
                }
                if (pointSheet.rate >= 60 && pointSheet.rate < 80) {
                    status = 2;
                    continue;
                } else if (pointSheet.rate >= 80) {
                    if (status == 2) {
                        continue;
                    }
                    status = 3;
                    continue;
                }
            }
        }
        List<String> knowNames = new ArrayList<String>();
        for (PointSheet pointSheet : pointSheets) {
            if (status == 1) {
                if (pointSheet.rate < 60) {
                    knowNames.add(pointSheet.knowName);
                }
            } else if (status == 2) {
                if (pointSheet.rate >= 60 && pointSheet.rate < 80) {
                    knowNames.add(pointSheet.knowName);
                }
            }
        }
        pointRemark.status = status;
        pointRemark.knowNames = knowNames;
        // return
        respModel.pointSheets = pointSheets;
        respModel.pointRemark = pointRemark;
        return respModel;
    }

    // 格式转化
    private Double getDouble(Double d1, int count) {
        if (count == 0) { return 0d; }
        if (d1 <= 0) { return 0d; }
        Double x1 = d1 / count;
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        double parse = Double.parseDouble(df.format(x1));
        return parse;
    }

}
